home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint96sb.zoo / src / console.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-10  |  4.8 KB  |  309 lines

  1. /*
  2. Copyright 1990,1991 Eric R. Smith. All rights reserved.
  3. */
  4.  
  5. /* MiNT routines for doing console I/O */
  6.  
  7. #include "mint.h"
  8.  
  9. /*
  10.  * These routines are what Cconout, Cauxout, etc. ultimately call.
  11.  * They take an integer argument which is the user's file handle,
  12.  * and do the translation to a file ptr (and return an appropriate error
  13.  * message if necessary.
  14.  * "mode" may be RAW, COOKED, ECHO, or COOKED|ECHO.
  15.  * note that the user may not call them directly!
  16.  */
  17.  
  18. long
  19. file_instat(f)
  20.     FILEPTR *f;
  21. {
  22.     long r;
  23.  
  24.     if (!f) {
  25.         return EIHNDL;
  26.     }
  27.     r = 1;        /* default is to assume input waiting (e.g. TOS files)*/
  28.     (void)(*f->dev->ioctl)(f, FIONREAD, &r);
  29.  
  30.     return r;
  31. }
  32.  
  33. long
  34. file_outstat(f)
  35.     FILEPTR *f;
  36. {
  37.     long r;
  38.  
  39.     if (!f) {
  40.         return EIHNDL;
  41.     }
  42.     r = 1;        /* default is to assume output OK (e.g. TOS files) */
  43.     (void)(*f->dev->ioctl)(f, FIONWRITE, &r);
  44.     return r;
  45. }
  46.  
  47. long
  48. file_getchar(f, mode)
  49.     FILEPTR *f;
  50.     int mode;
  51. {
  52.     char c;
  53.     long r;
  54.  
  55.     if (!f) {
  56.         return EIHNDL;
  57.     }
  58.     if (is_terminal(f)) {
  59.         return tty_getchar(f, mode);
  60.     }
  61.     r = (*f->dev->read)(f, &c, 1L);
  62.     if (r != 1)
  63.         return MiNTEOF;
  64.     else
  65.         return ((long)c) & 0xff;
  66. }
  67.  
  68. long
  69. file_putchar(f, c, mode)
  70.     FILEPTR *f;
  71.     long c;
  72.     int mode;
  73. {
  74.     char ch;
  75.  
  76.     if (!f) {
  77.         return EIHNDL;
  78.     }
  79.     if (is_terminal(f)) {
  80.         return tty_putchar(f, c & 0x7fffffffL, mode);
  81.     }
  82.     ch = c & 0x00ff;
  83.     return (*f->dev->write)(f, &ch, 1L);
  84. }
  85.  
  86. /*
  87.  * OK, here are the GEMDOS console I/O routines
  88.  */
  89.  
  90. long ARGS_ON_STACK
  91. c_conin()
  92. {
  93.     return file_getchar(curproc->handle[0], COOKED|ECHO);
  94. }
  95.  
  96. long ARGS_ON_STACK
  97. c_conout(c)
  98.     int c;
  99. {
  100.     return file_putchar(curproc->handle[1], (long)c, COOKED);
  101. }
  102.  
  103. long ARGS_ON_STACK
  104. c_auxin()
  105. {
  106.     return file_getchar(curproc->handle[2], RAW);
  107. }
  108.  
  109. long ARGS_ON_STACK
  110. c_auxout(c)
  111.     int c;
  112. {
  113.     return file_putchar(curproc->handle[2], (long)c, RAW);
  114. }
  115.  
  116. long ARGS_ON_STACK
  117. c_prnout(c)
  118.     int c;
  119. {
  120.     return file_putchar(curproc->handle[3], (long)c, RAW);
  121. }
  122.  
  123. long ARGS_ON_STACK
  124. c_rawio(c)
  125.     int c;
  126. {
  127.     long r;
  128.     PROC *p = curproc;
  129.  
  130.     if (c == 0x00ff) {
  131.         if (!file_instat(p->handle[0]))
  132.             return 0;
  133.         r = file_getchar(p->handle[0], RAW);
  134.         if (r <= 0)
  135.             return 0;
  136.         return r;
  137.     }
  138.     else
  139.         return file_putchar(p->handle[1], (long)c, RAW);
  140. }
  141.  
  142. long ARGS_ON_STACK
  143. c_rawcin()
  144. {
  145.     return file_getchar(curproc->handle[0], RAW);
  146. }
  147.  
  148. long ARGS_ON_STACK
  149. c_necin()
  150. {
  151.     return file_getchar(curproc->handle[0],COOKED|NOECHO);
  152. }
  153.  
  154. long ARGS_ON_STACK
  155. c_conws(str)
  156.     const char *str;
  157. {
  158.     const char *p = str;
  159.     long cnt = 0;
  160.  
  161.     while (*p++) cnt++;
  162.     return f_write(1, cnt, str);
  163. }
  164.  
  165. long ARGS_ON_STACK
  166. c_conrs(buf)
  167.     char *buf;
  168. {
  169.     long size, r;
  170.     char *s;
  171.  
  172.     size = ((long)*buf) & 0xff;
  173.     r = f_read(0, size, buf+2);
  174.     if (r < 0) {
  175.         buf[1] = 0;
  176.         return r;
  177.     }
  178. /* if reading from a file, stop at first CR or LF encountered */
  179.     s = buf+2;
  180.     size = 0;
  181.     while(r-- > 0) {
  182.         if (*s == '\r' || *s == '\n')
  183.             break;
  184.         s++; size++;
  185.     }
  186.     buf[1] = (char)size;
  187.     return 0;
  188. }
  189.  
  190. long ARGS_ON_STACK
  191. c_conis()
  192. {
  193.     return -(!!file_instat(curproc->handle[0]));
  194. }
  195.  
  196. long ARGS_ON_STACK
  197. c_conos()
  198. {
  199.     return -(!!file_outstat(curproc->handle[1]));
  200. }
  201.  
  202. long ARGS_ON_STACK
  203. c_prnos()
  204. {
  205.     return -(!!file_outstat(curproc->handle[3]));
  206. }
  207.  
  208. long ARGS_ON_STACK
  209. c_auxis()
  210. {
  211.     return -(!!file_instat(curproc->handle[2]));
  212. }
  213.  
  214. long ARGS_ON_STACK
  215. c_auxos()
  216. {
  217.     return -(!!file_outstat(curproc->handle[2]));
  218. }
  219.  
  220. /* Extended GEMDOS routines */
  221.  
  222. long ARGS_ON_STACK
  223. f_instat(h)
  224.     int h;
  225. {
  226.     PROC *proc;
  227.     int fh = h;
  228.  
  229. #if O_GLOBAL
  230.     if (fh >= 100) {
  231.         proc = rootproc;
  232.         fh -= 100;
  233.     } else
  234. #endif
  235.         proc = curproc;
  236.  
  237.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  238.         DEBUG(("Finstat: bad handle %d", h));
  239.         return EIHNDL;
  240.     }
  241.     return file_instat(proc->handle[fh]);
  242. }
  243.  
  244. long ARGS_ON_STACK
  245. f_outstat(h)
  246.     int h;
  247. {
  248.     int fh = h;
  249.     PROC *proc;
  250. #if O_GLOBAL
  251.     if (fh >= 100) {
  252.         fh -= 100;
  253.         proc = rootproc;
  254.     } else
  255. #endif
  256.         proc = curproc;
  257.  
  258.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  259.         DEBUG(("Foutstat: bad handle %d", h));
  260.         return EIHNDL;
  261.     }
  262.     return file_outstat(proc->handle[fh]);
  263. }
  264.  
  265. long ARGS_ON_STACK
  266. f_getchar(h, mode)
  267.     int h, mode;
  268. {
  269.     int fh = h;
  270.     PROC *proc;
  271.  
  272. #if O_GLOBAL
  273.     if (fh >= 100) {
  274.         fh -= 100;
  275.         proc = rootproc;
  276.     } else
  277. #endif
  278.         proc = curproc;
  279.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  280.         DEBUG(("Fgetchar: bad handle %d", h));
  281.         return EIHNDL;
  282.     }
  283.     return file_getchar(proc->handle[fh], mode);
  284. }
  285.  
  286. long ARGS_ON_STACK
  287. f_putchar(h, c, mode)
  288.     int h;
  289.     long c;
  290.     int mode;
  291. {
  292.     int fh = h;
  293.     PROC *proc;
  294.  
  295. #if O_GLOBAL
  296.     if (fh >= 100) {
  297.         fh -= 100;
  298.         proc = rootproc;
  299.     } else
  300. #endif
  301.         proc = curproc;
  302.  
  303.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  304.         DEBUG(("Fputchar: bad handle %d", h));
  305.         return EIHNDL;
  306.     }
  307.     return file_putchar(proc->handle[fh], c, mode);
  308. }
  309.